package com.dd.cloud.user.DTO.km;

import cn.hutool.core.codec.Base64;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAliasType;
import com.thoughtworks.xstream.io.naming.NoNameCoder;
import com.thoughtworks.xstream.io.xml.DomDriver;
import lombok.Data;

/**
 * @author Lx
 * @version 1.0
 */
@Data
@XStreamAliasType("orderInfo")
public class SendPresBodyDTO {
    @XStreamAlias("head")
    private SendPresHeadDTO head;

    @XStreamAlias("data")
    private SendPresDataDTO data;

    /**
     * 转换xml
     */
    public String toSoapXml() {
        //new DomDriver("UTF-8", new NoNameCoder()) 解决下划线节点字段变成双下划线问题 reg_num 会变成 reg__num
        XStream xStream = new XStream(new DomDriver("UTF-8", new NoNameCoder()));
        //忽略xml多余参数
        xStream.ignoreUnknownElements();
        xStream.autodetectAnnotations(true);
        xStream.omitField(SendPresHeadDTO.class, "methodName");
        String xml = xStream.toXML(this);
        xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + xml;
        System.out.println("xml:\n" + xml);
        return "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                "<soap:Body><ns1:saveOrderInfo xmlns:ns1=\"http://factory.service.cxf.kangmei.com/\"><request>"
                + Base64.encode(xml) +
                "</request></ns1:saveOrderInfo></soap:Body></soap:Envelope>";
    }
}
